package com.educate.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.educate.servicebase.exceptionhander.EducateException;
import com.educate.serviceedu.entity.EduChapter;
import com.educate.serviceedu.entity.EduVideo;
import com.educate.serviceedu.entity.chapter.ChapterVo;
import com.educate.serviceedu.entity.chapter.VideoVo;
import com.educate.serviceedu.entity.vo.CourseInfoVo;
import com.educate.serviceedu.mapper.EduChapterMapper;
import com.educate.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.educate.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.Watchable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoById(String courseId) {
        //1根据课程id查出课程所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
        //2查出所有小结
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(wrapperVideo);
        //创建list集合最终封装数据
        List<ChapterVo> finalList=new ArrayList<>();
        //3遍历查询出来的章节list集合
        for (int i = 0; i < eduChapters.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChapters.get(i);
            //把eduChapter对象值复制到chapter里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo放到最终finalList集合中去
            finalList.add(chapterVo);
            //创建集合,用于封装章节的小节
            List<VideoVo> videoList=new ArrayList<>();
            //4遍历查询出来的小结list集合
            for (int m = 0; m < eduVideos.size(); m++) {
                //得到每个小节
                EduVideo eduVideo=eduVideos.get(m);
                //判断小结里面的chapterId是否和id一样
                if(eduVideo.getChapterId().equals(chapterVo.getId())){
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }
            }
            //把封装之后的小节list集合放到章节对象里面
            chapterVo.setChildren(videoList);
        }
        return finalList;
    }
    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId查询小节表
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        //判断
        if(count>0){
            throw new EducateException(20001,"不能删除");
        }else{

            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }
    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
